import * as api from '@/api'
import types from '../types.js'

// initial state

const state = {
  menu: {
    // 是否折叠
    fold: localStorage.getItem('menu_fold') === 'true'
  },
  username: localStorage.getItem(types.USER_NAME),
}

// getters
const getters = {

  getMenu: state => state.menu,
  username: state => {
    return state.username;
  }
}

// actions
const actions = {
  //用户信息需要从服务器上获取，以便及时更新账户信息
 
  async getUserInfo(context) {
    const username = context.getters.username;
    // const username = context.state.username;
    if (username) {
      const res = await api.user.getInfoByName(username);
      if (res.errno === 0) {
        return res.data;
      } else {
        console.log(res.errmsg)
        return false;
      }
      
    } else {
      return false;
    }
  }
  
}

// mutations
const mutations = {

  setMenuFlod (state, value) {
    localStorage.setItem('menu_fold', value)
    state.menu.fold = value ? 1 : 0
  },

  setUserName (state, value) {
    localStorage.setItem(types.USER_NAME, value)
    state.username = value
  },
  
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
